export class CTokenList implements DOMTokenList {
    private _tokenList: Array<string> = [];

    [index: number]: string;

    get length(): number {
        return this._tokenList.length;
    }

    get value(): string {
        return this._tokenList.join(' ');
    }

    [Symbol.iterator](): ArrayIterator<string> {
        return this._tokenList[Symbol.iterator]();
    }

    add(...tokens: string[]): void {
        this._tokenList.push(...tokens);
    }

    contains(token: string): boolean {
        return this._tokenList.indexOf(token) !== -1;
    }

    entries(): ArrayIterator<[number, string]> {
        return this._tokenList.entries();
    }

    forEach(callback: (value: string, key: number, parent: DOMTokenList) => void, thisArg?: any): void {
        for (let key = 0; key < this.length; key++) {
            const token = this._tokenList[key];
            callback.apply(thisArg, [token, key, this]);
        }
    }

    item(index: number): string | null {
        return this._tokenList[index];
    }

    keys(): ArrayIterator<number> {
        return this._tokenList.keys();
    }

    remove(...tokens: string[]): void {
        for (const token of tokens) {
            this._tokenList = this._tokenList.filter(t => t !== token);
        }
    }

    replace(token: string, newToken: string): boolean {
        let replaced: boolean = false;
        this._tokenList = this._tokenList.map(t => {
            if (t == token) {
                replaced = true;
                return newToken;
            }
            return t;
        });
        return replaced;
    }

    supports(_token: string): boolean {
        return false;
    }

    toggle(_token: string, _force?: boolean): boolean {
        return false;
    }

    values(): ArrayIterator<string> {
        return this._tokenList.values();
    }

}